package com.example.tourapp.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.tourapp.Pojo.Pojo_City;
import com.example.tourapp.R;
import com.example.tourapp.util.Util_Internet;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Date;

public class City extends AppCompatActivity {
    //组件
    private TextView c_date;
    private TextView c_temperature;
    private EditText c_edtext;
    private Button c_query;
    private ImageView c_cityimg;
    private TextView c_citytext;
    private TextView c_manner;
    private TextView c_content;
    //全局
    private String citytext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        initView();
        inifunction();
        //注册EventBus
        EventBus.getDefault().register(this);
    }

    private void initView() {
        c_date = (TextView) findViewById(R.id.c_date);
        c_temperature = (TextView) findViewById(R.id.c_temperature);
        c_edtext = (EditText) findViewById(R.id.c_edtext);
        c_query = (Button) findViewById(R.id.c_query);
        c_cityimg = (ImageView) findViewById(R.id.c_cityimg);
        c_citytext = (TextView) findViewById(R.id.c_citytext);
        c_manner = (TextView) findViewById(R.id.c_manner);
        c_content = (TextView) findViewById(R.id.c_content);

    }

    private void inifunction() {
        c_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String edtext = c_edtext.getText().toString().trim();
                if (TextUtils.isEmpty(edtext)) {
                    Toast.makeText(City.this, "请输入查询天气的城市", Toast.LENGTH_SHORT).show();
                    return;
                }
                //清空天气描述
                c_content.setText("");
                Util_Internet.Get_ok(edtext, City.this);
            }
        });
    }
    //获取EventBus返回的数据结果
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void r_data(String data)
    {
        Gson gson=new Gson();
        Pojo_City city_data = gson.fromJson(data, Pojo_City.class);
        c_date.setText(city_data.getResult().getFuture().get(1).getDate());
        c_temperature.setText("实时气温"+city_data.getResult().getRealtime().getTemperature()+"度");
        c_citytext.setText(city_data.getResult().getRealtime().getInfo());
        c_manner.setText(city_data.getResult().getRealtime().getDirect()+city_data.getResult().getRealtime().getPower());
        c_content.append("相对湿度:"+city_data.getResult().getRealtime().getHumidity()+"%\n\n");
        citytext= city_data.getResult().getRealtime().getInfo();
        new Thread(()->{
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    switch (citytext)
                    {
                        case "晴":
                            c_cityimg.setImageResource(R.mipmap.weathericon_condition_01);
                            break;
                        case "多云":
                            c_cityimg.setImageResource(R.mipmap.weathericon_condition_03);
                            break;
                        case "阴":
                            c_cityimg.setImageResource(R.mipmap.weathericon_condition_04);
                            break;
                        case "阵雨":
                            c_cityimg.setImageResource(R.mipmap.weathericon_condition_08);
                            break;
                        case "雷阵雨":
                            c_cityimg.setImageResource(R.mipmap.weathericon_condition_10);
                            break;
                        case "雨夹雪":
                            c_cityimg.setImageResource(R.mipmap.weathericon_condition_13);
                            break;
                        case "小雨":
                            c_cityimg.setImageResource(R.mipmap.weathericon_condition_08);
                            break;
                        case "大雨":
                            c_cityimg.setImageResource(R.mipmap.weathericon_condition_09);
                            break;
                        case "沙尘暴":
                            c_cityimg.setImageResource(R.mipmap.weathericon_condition_06);
                            break;
                    }
                    SimpleDateFormat format = new SimpleDateFormat("HH");
                    Date date = new Date();
                    Integer c_time = Integer.valueOf(format.format(date));
                    if (c_time>=18)
                    {
                        c_cityimg.setImageResource(R.mipmap.weathericon_condition_16);
                    }
                }
            });

        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}